package service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import dao.BaseDAO;

import entity.Schedule;
import entity.User;
import service.ScheduleService;

@Service("scheduleService")
public class ScheduleServiceImpl implements ScheduleService {

	//依赖注入DAO
	@Resource(name="baseDAO")
	private BaseDAO baseDAO;
	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	@Override
	public List<Schedule> listEvents(User user, Date start, Date end) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String hql = "from Schedule s where s.user.id= ' " + user.getId() 
				+ "' and s.start <= '" + sdf.format(end) + "' and s.end >= '"+sdf.format(start)+"'";
		return baseDAO.query(hql);
	}
	
	public Integer addEvents(Schedule schedule) {
		return baseDAO.saveEntity(schedule);
	}

	@Override
	public void updateEvents(Schedule schedule) {
		baseDAO.saveOrUpdate(schedule);
	}

	@Override
	public void deleteEvents(int id) {
		baseDAO.delById(Schedule.class, id);
	}
	
	
	
	
	
}
